//
//  LLProgressSetView.m
//  VideoPlayerProject
//
//  Created by admin on 2019/11/23.
//  Copyright © 2019 admin. All rights reserved.
//

#import "LLProgressSetView.h"
#import <Masonry.h>

@interface LLProgressSetView ()

@property (nonatomic, strong) UILabel *currentPlayTimeLabel;    ///当前播放时长
@property (nonatomic, strong) UILabel *totalVideoTimeLabel;     ///视频总长度
@property (nonatomic, strong) UIProgressView *progressView;     ///进度条

@end

@implementation LLProgressSetView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    [self.currentPlayTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self);
        make.right.equalTo(self.mas_centerX).mas_offset(-10);
    }];
    [self.totalVideoTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self);
        make.left.equalTo(self.currentPlayTimeLabel.mas_right);
    }];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.equalTo(self).mas_offset(-20);
        make.height.mas_equalTo(2);
        make.bottom.equalTo(self).mas_offset(-2);
    }];
}

- (void)setCurrentSecond:(NSInteger)currentSecond
{
    _currentSecond = currentSecond;
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:currentSecond];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    if (currentSecond/3600 >=1) {
        [formatter setDateFormat:@"HH:mm:ss"];
    }else{
        [formatter setDateFormat:@"00:mm:ss"];
    }
    NSString * showTimeNew = [formatter stringFromDate:d];
    self.currentPlayTimeLabel.text = showTimeNew;
    
    if (self.totalSecond > 0) {
        self.progressView.progress = (CGFloat)currentSecond / self.totalSecond;
    }
}

- (void)setTotalSecond:(NSInteger)totalSecond
{
    _totalSecond = totalSecond;
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:totalSecond];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    if (totalSecond/3600 >=1) {
        [formatter setDateFormat:@"HH:mm:ss"];
    }else{
        [formatter setDateFormat:@"00:mm:ss"];
    }
    NSString * showTimeNew = [formatter stringFromDate:d];
    self.totalVideoTimeLabel.text = [NSString stringWithFormat:@" / %@", showTimeNew];
}

- (UILabel *)currentPlayTimeLabel
{
    if (!_currentPlayTimeLabel) {
        _currentPlayTimeLabel = [[UILabel alloc] init];
        _currentPlayTimeLabel.textColor = [UIColor redColor];
        _currentPlayTimeLabel.font = [UIFont boldSystemFontOfSize:20];
        _currentPlayTimeLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:_currentPlayTimeLabel];
    }
    return _currentPlayTimeLabel;
}

- (UILabel *)totalVideoTimeLabel
{
    if (!_totalVideoTimeLabel) {
        _totalVideoTimeLabel = [[UILabel alloc] init];
        _totalVideoTimeLabel.textColor = [UIColor whiteColor];
        _totalVideoTimeLabel.font = [UIFont boldSystemFontOfSize:20];
        [self addSubview:_totalVideoTimeLabel];
    }
    return _totalVideoTimeLabel;
}

- (UIProgressView *)progressView
{
    if (!_progressView) {
        _progressView = [[UIProgressView alloc] init];
        _progressView.progress = 0.0f;
        _progressView.progressTintColor = [UIColor redColor];
        _progressView.trackTintColor = [UIColor whiteColor];
        [self addSubview:_progressView];
    }
    return _progressView;
}

@end
